package QueryPermissions;

import UserAndGroup.Creat;
import org.activiti.engine.IdentityService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.identity.Group;
import org.activiti.engine.identity.User;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.task.IdentityLink;

import java.util.List;

/**
 * <p>类描述：查询权限数据。</p>
 *
 * @author 贾亮
 * @version v1.0.0.1。
 * @since JDK1.8。
 * <p>创建日期：2018/3/6 17:50。</p>
 */
public class Main {
    public static void main(String[] args) {
        // 创建流程引擎
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        // 得到流程存储服务组件
        RepositoryService repositoryService = processEngine.getRepositoryService();
        // 得到身份服务组件实例
        IdentityService identityService = processEngine.getIdentityService();
        // 部署流程描述文件
        Deployment deployment = repositoryService.createDeployment().addClasspathResource("MyProcess.bpmn").deploy();
        // 添加两个用户
        User user1 = Creat.creatUser(identityService, "user1", "张三", "张三", "yangenxiong@163.com", "abc");
        User user2 = Creat.creatUser(identityService, "user2", "李四", "李四", "yangenxiong@163.com", "abc");
        // 添加两个用户组
        Group group1 = Creat.createGroup(identityService, "group1", "经理组", "manager");
        Group group2 = Creat.createGroup(identityService, "group2", "员工组", "employee");
        // 查询流程定义
        ProcessDefinition def = repositoryService.createProcessDefinitionQuery().deploymentId(deployment.getId()).singleResult();
        // 设置权限数据
        repositoryService.addCandidateStarterUser(def.getId(), user1.getId());
        repositoryService.addCandidateStarterUser(def.getId(), user2.getId());
        repositoryService.addCandidateStarterGroup(def.getId(), group1.getId());
        repositoryService.addCandidateStarterGroup(def.getId(), group2.getId());
        // 根据用户查询有权限的流程定义
        List<ProcessDefinition> defs = repositoryService.createProcessDefinitionQuery().startableByUser("user1").list();
        System.out.println("用户张三有权限的流程定义为：");
        for (ProcessDefinition pd : defs) {
            System.out.println(pd.getName());
        }
        // 根据流程定义查询用户数据
        List<User> users = identityService.createUserQuery().potentialStarter(def.getId()).list();
        System.out.println("以下用户对流程定义有权限：");
        for (User user : users) {
            System.out.println(user.getFirstName());
        }
        // 根据流程定义查询用户组数据
        List<Group> groups = identityService.createGroupQuery().potentialStarter(def.getId()).list();
        System.out.println("以下用户组对流程定义有权限：");
        for (Group group : groups) {
            System.out.println(group.getName());
        }
        // 根据流程定义查询全部的IdentityLink(act_ru_identitylink)数据
        List<IdentityLink> identityLinks = repositoryService.getIdentityLinksForProcessDefinition(def.getId());
        System.out.println("与流程定义相关的数据量：" + identityLinks.size());
        processEngine.close();
    }
}
